using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
using Common;

public partial class tm_UC_TreeTab : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TreeView1.Attributes.Add("onclick", "OnTreeClick(event)");

            PopulateRootLevel("02");

        }
    }

    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        PopulateSubLevel(e.Node.Value, e.Node);

        TreeView treeView = TreeView1 as TreeView;
        TreeNodeCollection items = treeView.Nodes;
        //check all
        CheckAll(items);

    }

    protected void CheckAll(TreeNodeCollection items)
    {
        if (Session["tm_arrayList"] != null)
        {
            TreeView treeView = TreeView1 as TreeView;

            ArrayList al = (ArrayList)Session["tm_arrayList"];

            if ((treeView != null) && (items != null))
            {
                foreach (TreeNode item in items)
                {
                    for (int i = 0; i < al.Count; i++)
                    {
                        if (al[i].ToString().Trim() == item.Value.ToString().Trim())
                            item.Checked = true;
                        foreach (TreeNode temp in item.ChildNodes)
                        {
                            if (al[i].ToString().Trim() == temp.Value.ToString().Trim())
                            {
                                temp.Checked = true;
                            }
                            else CheckAll(temp.ChildNodes);

                        }
                    }
                }
            }
        }
    }
    protected void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
        nodes.Clear();
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            tn.Text = dr["description1"].ToString().Trim();
            tn.Value = dr["spcCode"].ToString().Trim();
            nodes.Add(tn);

            //If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
        }
    }
    protected void PopulateRootLevel(string parentCode)
    {
        SqlConnection objConn = new SqlConnection(sysLibs.GetConnectionString);
        SqlCommand objCommand = new SqlCommand(@"select description1, spcCode,(select count(*) FROM tm_spccode WHERE parentCode=sc.spcCode ) childnodecount FROM tm_spccode sc WHERE     (subGroup =N'" + parentCode.Trim() + "') ", objConn);
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, TreeView1.Nodes);
    }

    protected void PopulateSubLevel(string ParentCode, TreeNode parentNode)
    {
        SqlConnection objConn = new SqlConnection(sysLibs.GetConnectionString);


        SqlCommand objCommand = new SqlCommand(@"select description1, spcCode,(select count(*) FROM tm_spccode WHERE ParentCode=sc.spcCode) childnodecount FROM tm_spccode sc where (parentCode=@ParentCode)", objConn);
        objCommand.Parameters.Add("@ParentCode", SqlDbType.Char, 10).Value = ParentCode;
        //objCommand.Parameters.Add("@subGroup", SqlDbType.Char, 5).Value = DropDownList1.SelectedValue;
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, parentNode.ChildNodes);
    }
    //end show treeview

    protected void btnChon_Click(object sender, EventArgs e)
    {
        Hashtable tm_array = new Hashtable();
        ArrayList tm_arrayList = new ArrayList();

        if (TreeView1.CheckedNodes.Count > 0)
        {

            foreach (TreeNode item in TreeView1.CheckedNodes)
            {
                tm_array.Add(item.Value.ToString(), item.Text.ToString());
                tm_arrayList.Add(item.Value.ToString().Trim());
            }
        }


        //return array Hashtable
        Session["tm_ReturnArray"] = tm_array;

        //return array List
        Session["tm_arrayList"] = tm_arrayList;

        //khoi
        Session["tm_ReturnArrayTT"] = tm_array;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TreeView1_Unload(sender, e);
    }

    protected void TreeView1_Unload(object sender, EventArgs e)
    {
        string subGroup = DropDownList1.SelectedValue.ToString();
        PopulateRootLevel(subGroup.Trim());
    }
}
